package com.campus.counseling.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.counseling.model.common.Result;
import com.campus.counseling.model.dto.ArticleDTO;
import com.campus.counseling.model.query.ArticleQuery;
import com.campus.counseling.model.vo.ArticleVO;
import com.campus.counseling.service.ArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Tag(name = "文章管理")
@RestController
@RequestMapping("/api/articles")
@RequiredArgsConstructor
public class ArticleController {

    private final ArticleService articleService;

    @GetMapping
    @Operation(summary = "获取文章列表")
    public Result<Page<ArticleVO>> list(ArticleQuery query) {
        return Result.success(articleService.list(query));
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取文章详情")
    public Result<ArticleVO> getArticle(@PathVariable Long id) {
        return Result.success(articleService.getArticle(id));
    }

    @PostMapping
    @Operation(summary = "新增文章")
    @PreAuthorize("hasAnyRole('admin', 'superadmin')")
    public Result<Void> add(@RequestBody ArticleDTO articleDTO) {
        articleService.add(articleDTO);
        return Result.success();
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新文章")
    @PreAuthorize("hasAnyRole('admin', 'superadmin')")
    public Result<Void> update(@PathVariable Long id, @RequestBody ArticleDTO articleDTO) {
        articleService.update(id, articleDTO);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除文章")
    @PreAuthorize("hasAnyRole('admin', 'superadmin')")
    public Result<Void> delete(@PathVariable Long id) {
        articleService.delete(id);
        return Result.success();
    }

    @PutMapping("/{id}/order")
    @Operation(summary = "更新文章排序")
    @PreAuthorize("hasAnyRole('admin', 'superadmin')")
    public Result<Void> updateOrder(@PathVariable Long id, @RequestParam Integer orderNum) {
        articleService.updateOrder(id, orderNum);
        return Result.success();
    }
} 